package org.example.algorithm.LinkList;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 简单LRU：
 * Hash表 + 双端链表
 * 实现思路：利用双端链表扩展。
 */
public class SimpleLRUCache extends LinkedHashMap<Integer, Integer> implements ILRUCache {

    private final int capacity;

    public SimpleLRUCache(int capacity){
        super(capacity, 0.75F, true);
        this.capacity = capacity;
    }

    @Override
    public int get(int key) {
        return super.getOrDefault(key, -1);
    }

    @Override
    public void put(int key, int value) {
        super.put(key,value);
    }

    @Override
    protected boolean removeEldestEntry(Map.Entry<Integer, Integer> eldest) {
        return size() > capacity;
    }
}
